package com.lyy.photovoltaic.service.impl;

import com.lyy.photovoltaic.beans.Equipments;
import com.lyy.photovoltaic.beans.UsersEquipments;
import com.lyy.photovoltaic.common.utils.ConvertUtil;
import com.lyy.photovoltaic.common.vo.ResultCode;
import com.lyy.photovoltaic.common.vo.ResultVO;
import com.lyy.photovoltaic.common.vo.UsersEquipmentsVo;
import com.lyy.photovoltaic.mapper.EquipmentsMapper;
import com.lyy.photovoltaic.mapper.UsersEquipmentsMapper;
import com.lyy.photovoltaic.service.UserEquipmentsListsService;
import com.lyy.photovoltaic.service.UserEquipmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;


import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserEquipmentsServiceImpl implements UserEquipmentsService {
    @Autowired
    UsersEquipmentsMapper usersEquipmentsMapper;
    @Autowired
    UserEquipmentsListsService userEquipmentsListsService;
    @Autowired
    EquipmentsMapper equipmentsMapper;
    @Override
    public int addUsersEquipments(List list) {
        int i = usersEquipmentsMapper.batchInsert(list);
        if (i > 0) {
            return i;
        }
       return 0;
    }

    @Override
    public ResultVO allUserEquipments() {
        List<UsersEquipments> usersEquipments = usersEquipmentsMapper.selectAll();
        if (usersEquipments != null) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS,"查询用户设备成功",usersEquipments);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED,"查询用户设备失败",null);
    }

    @Override
    public ResultVO abnormalUserEquipments() {
        Example example = new Example(UsersEquipments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("peStatus",false);
        List<UsersEquipments> usersEquipments = usersEquipmentsMapper.selectByExample(example);
        if (usersEquipments.size() > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS,"查询异常用户设备成功",usersEquipments);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED,"查询异常用户设备失败",null);
    }

    @Override
    public ResultVO userEquipmentsByUId(String uId) {
        List<String> ueIdList = userEquipmentsListsService.ueIdList(uId);
        List<UsersEquipments> usersEquipments = usersEquipmentsMapper.batchFind(ueIdList);
        List<UsersEquipmentsVo> usersEquipmentsVo = ConvertUtil.entityToVoList(usersEquipments, UsersEquipmentsVo.class);
//        System.out.println(usersEquipmentsVo);

        List<String> eIdLists = usersEquipments.stream().map(UsersEquipments::getEquipmentId).collect(Collectors.toList());
//        System.out.println(eIdLists);
//        System.out.println(usersEquipments);
        List<Equipments> equipmentsByList = equipmentsMapper.findEquipmentsByList(eIdLists);
//        System.out.println("equipmentsByList---"+equipmentsByList);
        List<String> eNameLists = equipmentsByList.stream().map(Equipments ::geteName).collect(Collectors.toList());
        System.out.println(eNameLists);
//        for (String eNameList:eNameLists){
//            usersEquipmentsVo.forEach(p->p.setEName(eNameList));
//        }
        for (int i = 0; i < usersEquipmentsVo.size(); i++) {
            usersEquipmentsVo.get(i).setEName(eNameLists.get(i));
        }
        System.out.println(usersEquipmentsVo);
        List<String> categoryIdList = equipmentsByList.stream().map(Equipments::getCategoryId).collect(Collectors.toList());
        for (int i = 0; i < usersEquipmentsVo.size(); i++) {
            usersEquipmentsVo.get(i).setCategoryId(categoryIdList.get(i));
        }
        System.out.println(usersEquipmentsVo);
        if (usersEquipments.size() > 0){
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS,"根据用户Id查常用户设备成功",usersEquipmentsVo);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED,"根据用户Id查询用户设备失败",null);
    }
}
